package org.leviatan.common.config;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class Configuration {	
	
	private Properties props;

	public Configuration() {
		this.props = new Properties();
	}
	
	public void loadConfigurationFromXml(String xmlFile) {
		FileInputStream fr = null;
		try {
			fr = new FileInputStream(xmlFile);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		
		BufferedInputStream bis = new BufferedInputStream(fr);
		
		try {
			props.loadFromXML(bis);
		} catch (InvalidPropertiesFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void saveConfigurationAsXml(String xmlFile) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(xmlFile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		
		try {
			this.props.storeToXML(bos, "org.leviatan.common.config.Configuration properties.");
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	public void addProperty(String key, String value) {
		this.props.put(key, value);
	}
	
	public String getProperty(String key) {
		return this.props.getProperty(key);
	}
	
	
}
